import json

import requests

import time

import pymysql  # 调用模块


# -----------------------------连接本地Mysql数据(start)---------------------------
db = pymysql.connect(user='root', password='数据库密码', database='qfnu', charset='utf8')  # 打开数据库连接
cursor = db.cursor()  # 获取操作游标
demo = cursor.execute('select * from qfnu_table')  # 运行SOL语句
lists = cursor.fetchall()  # 接收返回的结果
# --------------------------------------end--------------------------------------


# print(lists)
s = ""

for i in lists:
    print(i)
    # username = '账号'
    username = i[0]
    # password = '密码'
    password = i[1]
    # 登录抓包
    url1 = "http://xuegong.qfnu.edu.cn:8080/authentication/login"
    header1 = {
        'Host': 'xuegong.qfnu.edu.cn:8080',
        'User-Agent': 'Dart/2.10(dart:io)',
        'Accept-Encoding': 'gzip',
        'Content-Type': 'application/json',
        'Content-Length': '66'
    }
    data1 = {"username": username, "password": password, "type": "student"}
    r1 = requests.request("post", url1, json=data1, headers=header1)
    r1 = json.loads(r1.text)
    uuid = r1['data']

    # 获取个人信息
    url2 = "http://xuegong.qfnu.edu.cn:8080/student/healthInfo/todayInfo"

    try:
        header2 = {
            'Host': 'xuegong.qfnu.edu.cn:8080',
            'User-Agent': 'Dart/2.10(dart:io)',
            'Accept-Encoding': 'gzip',
            'cookie': 'syt.sessionId=' + uuid,
            'authorization': uuid
        }
        r2 = requests.post(url=url2, headers=header2)
    except:

        demo = cursor.execute("delete from qfnu_table where username = '{}'".format(username))  # 运行SOL语句
        lists = cursor.fetchall()  # 接收返回的结果
        db.commit()
        # s = s + '账号：' + i[0] + ',密码：' + i[1] + '不正确的用户名和密码' + '\n'
        s = s + '账号：' + i[0] + '，无效的账号和密码\n'
        print('不正确的用户名和密码')
        continue


    txt = r2.text

    complexinfo = json.loads(txt)['data']

    try:
        address = complexinfo['address']
    except:
        # s = s + '账号：' + i[0] + ',密码：' + i[1] + '地址获取异常'+ '\n'
        s = s + '账号：' + i[0] + '，数据获取异常\n'
        print('地址获取异常')
        continue

    location = complexinfo['location']
    home = complexinfo['home']
    selfinfo = {
        "home": home,
        "address": address,
        "keepInHome": "否",
        "keepInHomeDate": 'null',
        "keepInHomeReasonSite": "",
        "contact": "否",
        "contactType": "",
        "infect": "否",
        "infectType": "",
        "infectDate": "",
        "familyNCP": "否",
        "familyNCPType": "",
        "familyNCPDate": "",
        "familyNCPRelation": "",
        "cold": "否",
        "fever": "否",
        "feverValue": "",
        "cough": "否",
        "diarrhea": "否",
        "homeInHubei": "否",
        "arriveHubei": "无",
        "travel": "无",
        "remark": "无",
        "submitCount": 1,
        "contactDetail": "",
        "location": location,
        "naDetection": "否",
        "areaInfect": "否",
        "areaInfectType": "",
        "areaInfectDate": "",
        "areaInfectNumber": "",
        "contactAH": "否",
        "contactAHDetail": "",
        "outProvinceBack14": "未出省",
        "naDetectionDate": "",
        "pharynxResult": "阴性",
        "anusResult": "阴性",
        "saDetection": "否",
        "lgMResult": "阴性",
        "lgGResult": "阴性",
        "saDetectionDate": ""
    }
    selfinfo = json.dumps(selfinfo)

    # 提交健康信息

    url3 = "http://xuegong.qfnu.edu.cn:8080/student/healthInfo/save"
    header3 = {
        'Host': 'xuegong.qfnu.edu.cn:8080',
        'User-Agent': 'Dart/2.10(dart:io)',
        'Accept-Encoding': 'gzip',
        'Content-Type': 'application/json',
        'cookie': 'syt.sessionId=' + uuid,
        'authorization': uuid
    }
    r3 = requests.post(url3, headers=header3, data=selfinfo)
    # s = s + '账号：' + i[0] + ',密码：' + i[1] + r3.text + '\n'
    s = s + '账号：' + i[0] + '，签到成功。\n'
    # print('--->' + s)
    print(r3.text)


# ------------------------Server酱·Turbo版(start)--------------------------------
# def push(SCKEY, msg):
#     if SCKEY.isspace() or len(SCKEY) == 0:
#         return
#     else:
#         # api = 'https://sc.ftqq.com/' + SCKEY + '.send'
#         api = 'https://sctapi.ftqq.com/' + SCKEY + '.send'
#         title = u"签到情况"
#         content = msg
#         data = {
#             "text": title,
#             "desp": msg
#         }
#         req = requests.post(api, data=data)
#
#
# push('此处填写Server酱·Turbo版个人的SCKEY', s)
# ---------------------------------end-------------------------------------------


#--------------------------------酷推QQ私有化个人消息(start)----------------------
# url = "http://127.0.0.1:5700/send_private_msg"

# datas = {
# 	"user_id":"个人QQ号", 
# 	"message":""+"签到情况： \n" + s
# }
# print(s)


# requests.post(url, datas)
#----------------------------------end-----------------------------------------
 

#--------------------------------酷推QQ私有化群发消息(start)----------------------
# url = "http://127.0.0.1:5700/send_group_msg"

# datas = {
# 	"group_id":"群号", 
# 	"message":""+"签到情况： \n" + s
# }
# print(s)


# requests.post(url, datas)
#----------------------------------end-----------------------------------------



# --------------------------------pushplus(start)-------------------------------
#url = "http://pushplus.hxtrip.com/send?token=个人token密钥"
#
#datas = {
#	"title":"每日健康签到", 
#	"template":"json", 
#    "content": ""+"签到情况： \r\n" + s
#}
#
#print(s)
#
#requests.post(url, datas)
# ----------------------------------end-----------------------------------------



# --------------------------------酷推QQ消息(start)-------------------------------
# SCKEY = "此处填写酷推个人的SCKEY"
# url = "https://push.xuthus.cc/send/" + SCKEY

# datas = "签到情况： \n" + s

# print(s)

# data = datas.encode('utf-8')

# requests.post(url, data)
# ----------------------------------end-----------------------------------------


# --------------------------------Qmsg(start)-------------------------------
# url = "https://qmsg.zendee.cn/send/个人密钥"

# datas = {
#     "msg": ""+"签到情况： \n" + s
# }

# print(s)

# requests.get(url, datas)
# ----------------------------------end-----------------------------------------

# --------------------------------本地时间打印(start)-------------------------------
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
print("---------------------------------------------------------------------------")
# ----------------------------------end-----------------------------------------
